package manage.student.view;

import manage.entity.StudentDo;
import manage.handler.AddStudentViewHandler;
import manage.student.MainView;

import javax.swing.*;
import java.awt.*;

public class AddStudentView extends JDialog {
    JPanel jPanel=new JPanel(new FlowLayout(FlowLayout.CENTER,10,20));
    JLabel nameLabel=new JLabel("姓名",JLabel.RIGHT);
    JTextField nameText=new JTextField();
    JLabel sexLabel=new JLabel("性别",JLabel.RIGHT);
    JTextField sexText=new JTextField();
    JLabel gradeLabel=new JLabel("班级",JLabel.RIGHT);
    JTextField gradeText=new JTextField();
    JLabel ChineseLabel=new JLabel("语文成绩",JLabel.RIGHT);
    JTextField ChineseText=new JTextField();
    JLabel MathLabel=new JLabel("数学成绩",JLabel.RIGHT);
    JTextField MathText=new JTextField();
    JLabel EnglishLabel=new JLabel("英语成绩",JLabel.RIGHT);
    JTextField EnglishText=new JTextField();
    JButton addBtn=new JButton("添加");

    AddStudentViewHandler addStudentViewHandler;



    public AddStudentView(MainView mainView){
        super(mainView,"添加学生",true);
        addStudentViewHandler=new AddStudentViewHandler(this,mainView);
        //添加组件
        nameLabel.setPreferredSize(new Dimension(80,30));
        nameText.setPreferredSize(new Dimension(200,30));
        jPanel.add(nameLabel);
        jPanel.add(nameText);

        sexLabel.setPreferredSize(new Dimension(80,30));
        sexText.setPreferredSize(new Dimension(200,30));
        jPanel.add(sexLabel);
        jPanel.add(sexText);
        gradeLabel.setPreferredSize(new Dimension(80,30));
        gradeText.setPreferredSize(new Dimension(200,30));
        jPanel.add(gradeLabel);
        jPanel.add(gradeText);
        ChineseLabel.setPreferredSize(new Dimension(80,30));
        ChineseText.setPreferredSize(new Dimension(200,30));
        jPanel.add(ChineseLabel);
        jPanel.add(ChineseText);
        MathLabel.setPreferredSize(new Dimension(80,30));
        MathText.setPreferredSize(new Dimension(200,30));
        jPanel.add(MathLabel);
        jPanel.add(MathText);
        EnglishLabel.setPreferredSize(new Dimension(80,30));
        EnglishText.setPreferredSize(new Dimension(200,30));
        jPanel.add(EnglishLabel);
        jPanel.add(EnglishText);


        addBtn.addActionListener(addStudentViewHandler);
        jPanel.add(addBtn);
        //
        Container container=getContentPane();
        container.add(jPanel);


        setSize(350,500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false);
        setVisible(true);
    }

    public StudentDo buildStudent(){
        StudentDo studentDo=new StudentDo();
        studentDo.setName(nameText.getText());
        studentDo.setSex(sexText.getText());
        studentDo.setGrade(gradeText.getText());
        studentDo.setChinese(Integer.parseInt(ChineseText.getText()));   //!!!注意 JTextfield获取数据,需要用包装类转换
        studentDo.setMath(Integer.parseInt(MathText.getText()));
        studentDo.setEnglish(Integer.parseInt(EnglishText.getText()));
        return studentDo;
    }
}
